﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "CmdScaleGraphicsNodeStyle.h"
#include "IUiView.h"
#include "IElement.h"
#include "IDocument.h"
#include "ElementId.h"
#include "ISelection.h"
#include "IModelView.h"
#include "IUiDocument.h"
#include "IDirectShape.h"
#include "IGraphicsNode.h"
#include "UiCommonDialog.h"
#include "IGenericElement.h"
#include "GcmpCommandState.h"
#include "IUserTransaction.h"
#include "CommandParameters.h"
#include "IElementModelShape.h"
#include "UiDocumentViewUtils.h"
#include "IGraphicsElementShape.h"
#include "IUiDocumentViewManager.h"
#include "IGraphicsNodePathCollector.h"
#include "IElementViewSpecificShapeComponent.h"
#include "IGraphicsNodeStyleAndMaterialOverride.h"

#include <unordered_set>
#include "GRepCommandIds.h"
#include "CommandRegister.h"
#include "ScaleGraphicsNodeStyleCustomizer.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace gcmp;
using namespace Sample;

// 【视图比例动态样式】命令
REGISTER_COMMAND(CmdScaleGraphicsNodeStyle)

Sample::CmdScaleGraphicsNodeStyle::CmdScaleGraphicsNodeStyle()
    : CommandBase(ID_CMD_SCALE_GSTYLE)
{
}

bool Sample::CmdScaleGraphicsNodeStyle::IsEnabled() const
{
    if (!GcmpCommandState::IsInDocumentEnvironment())
    {
        return false;
    }
    // 有选中的图元
    if (ISelection* pSelection = ISelection::Get())
    {
        std::unordered_set<ElementId, ElementIdHash> elemMaps = pSelection->GetSelectedElements();
        if (elemMaps.size() > 0)
        {
            return true;
        }
    }
    return false;
}

bool Sample::CmdScaleGraphicsNodeStyle::IsVisible() const
{
    return GcmpCommandState::IsInDocumentEnvironment();
}

gcmp::OwnerPtr<gcmp::IAction> Sample::CmdScaleGraphicsNodeStyle::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc 为空",L"GDMP",L"2024-03-30");
    ISelection* pSelection = ISelection::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pSelection, L"pSelection为空",L"GDMP",L"2024-03-30");
    std::unordered_set<ElementId, ElementIdHash> elemMaps = pSelection->GetSelectedElements();

    OwnerPtr<IUserTransaction> opUT(IUserTransaction::Create(pDoc, L"注册样式定制器"));
    bool isSet = false;
    for (auto elemId : elemMaps)
    {
        IElement* pElement = pDoc->GetElement(elemId);
        DBG_WARN_AND_CONTINUE_UNLESS(pElement, L"pElement为空",L"GDMP",L"2024-03-30");

        // 获得图形节点样式和材质的重载组件
        IGraphicsNodeStyleAndMaterialOverride* pOverride = pElement->GetShapeGraphicsNodeStyleAndMaterialOverrideComponentFW();
        if (!pOverride)
            continue;
        OwnerPtr<ScaleGraphicsNodeStyleCustomizer> opCustomizer = NEW_AS_OWNER_PTR(ScaleGraphicsNodeStyleCustomizer, pElement);
        DBG_WARN_AND_CONTINUE_UNLESS(opCustomizer, L"opCustomizer为空",L"GDMP",L"2024-03-30");

        // 功能实现：将样式回调派生实现对象，注册到对应图元的图形节点样式和材质的重载组件里
        pOverride->SetGraphicsNodeStyleAndMaterialCustomizer(TransferOwnership(opCustomizer));
        isSet = true;
    }

    if (!isSet)
    {
        UiCommonDialog::ShowMessageBox(L"提示", 
            L"选择的图元均无法重载模型图形表达：均没有图形节点样式和材质的重载组件 - IGraphicsNodeStyleAndMaterialOverride", (int)UiCommonDialog::ButtonType::Yes);
        if (opUT->IsStarted()) opUT->Rollback();
        return nullptr;
    }

    opUT->Commit();
    return nullptr;
}

